import { useUserStore } from '@/stores/user'
import type { Directive, DirectiveBinding } from 'vue'

// 管理员权限
const adminPerms = '*:*:*'

export const auth: Directive = {
  mounted(el: HTMLElement, binding: DirectiveBinding<string>) {
    const { value } = binding
    const store = useUserStore()
    const perms = store.profile.permissions

    if (perms.includes(adminPerms)) {
      // 如果是管理员，不做任何处理
      return
    }

    if (!perms.includes(value)) {
      // 当前用户没有权限，移除该元素
      el.remove()
    }
  },
}
